import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  DefaultValuePipe,
  Put,
} from '@nestjs/common';
import { EnterpriseService } from './enterprise.service';
import { CreateEnterpriseDto } from './dto/create-enterprise.dto';
import { UpdateEnterpriseDto } from './dto/update-enterprise.dto';
import { generateParseIntPipe } from 'src/utils';
import { EnterpriseBackgroundData } from './types/EnterpriseBackgroundData';
import { CreatePatentDto } from './dto/create-patent.dto';
import { UpdatePatentDto } from './dto/update-patent.dto';
import { CreateTrademarkDto } from './dto/create-trademark.dto';
import { UpdateTrademarkDto } from './dto/update-trademark.dto';
import { CreateExecutiveDto } from './dto/create-executive.dto';
import { UpdateExecutiveDto } from './dto/update-executive.dto';
import { RequireLogin } from 'src/custom.decorator';

@Controller('enterprise')
export class EnterpriseController {
  constructor(private readonly enterpriseService: EnterpriseService) {}

  // 初始化数据
  @Get('init-data')
  async initData() {
    await this.enterpriseService.initData();
    return 'done';
  }

  // 获取企业列表，模糊查询
  @Get('list')
  async searchEnterprises(
    @Query('name') name: string,
    @Query('registrationNo') registrationNo: string,
    @Query('legalRepresentative') legalRepresentative: string,
    @Query('registeredCapital') registeredCapital: string,
    @Query('registeredAddress') registeredAddress: string,
    @Query('operationStatus') operationStatus: string,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(5),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    return await this.enterpriseService.findEnterprisesList(
      name,
      registrationNo,
      legalRepresentative,
      registeredCapital,
      registeredAddress,
      operationStatus,
      pageNo,
      pageSize,
    );
  }
  @Get('find')
  async findEnterprise() {
    return await this.enterpriseService.findEnterprise();
  }

  // 新增企业信息
  @Post('create')
  async create(@Body() createEnterpriseDto: CreateEnterpriseDto) {
    await this.enterpriseService.create(createEnterpriseDto);
    return 'success';
  }

  // 更新企业信息
  @Put('update')
  async update(@Body() updateEnterpriseDto: UpdateEnterpriseDto) {
    return await this.enterpriseService.update(updateEnterpriseDto);
  }

  // 根据id查询企业信息
  @Get('getDetail/:enterpriseId/:userId')
  async find(
    @Param('userId') userId: number,
    @Param('enterpriseId') enterpriseId: number,
  ) {
    return await this.enterpriseService.findById(userId, enterpriseId);
  }

  // 删除企业信息
  @Delete('delete/:id')
  async delete(@Param('id') id: number) {
    await this.enterpriseService.delete(id);
  }

  // 查询企业背景
  @Get('findEnterpriseBackground/:enterpriseId')
  async getEnterpriseBackgroundById(
    @Param('enterpriseId') enterpriseId: number,
  ) {
    return await this.enterpriseService.getEnterpriseBackgroundById(
      enterpriseId,
    );
  }

  // 获取企业背景列表
  @Get('/background/list')
  async getEnterpriseBackgroundList(
    @Query('name') name: string,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(5),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    return this.enterpriseService.getEnterpriseBackgroundList(
      name,
      pageNo,
      pageSize,
    );
  }

  // 获取行业排行榜
  @Get('industries/:industryId/rankings')
  async getIndustryRankings(@Param('industryId') industryId: number) {
    const result = await this.enterpriseService.getIndustryRankings(industryId);
    return result;
  }

  // 获取所有行业排行榜
  @Get('industries/rankings')
  async getAllIndustriesRankings() {
    const result = await this.enterpriseService.getAllIndustriesRankings();
    return result;
  }

  // 创建企业背景信息
  @Post('background')
  async createBackground(@Body() background: EnterpriseBackgroundData) {
    const result = await this.enterpriseService.createBackground(background);
    return 'success';
  }

  // 更新企业背景信息
  @Put('background/:id')
  async updateBackground(
    @Param('id') id: number,
    @Body() background: EnterpriseBackgroundData,
  ) {
    const result = await this.enterpriseService.updateBackground(
      id,
      background,
    );
    return 'success';
  }

  // 删除企业背景信息
  @Delete('background/:id')
  async deleteBackground(@Param('id') id: number) {
    const result = await this.enterpriseService.deleteBackground(id);
    return 'success';
  }

  // 获取行业列表
  @Get('industry/list')
  async getAllIndustries(
    @Query('name') name: string,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(10),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    return this.enterpriseService.getAllIndustries(name, pageNo, pageSize);
  }
  @Get('industry/allList')
  async getAllIndustryList() {
    return this.enterpriseService.getIndustries();
  }

  // 创建行业
  @Post('industry/create')
  async createIndustry(@Body() body: { name: string; description?: string }) {
    const { name, description } = body;
    return this.enterpriseService.createIndustry(name, description);
  }

  // 更新行业
  @Put('industry/update/:id')
  async updateIndustry(
    @Param('id') id: number,
    @Body() body: { name: string; description?: string },
  ) {
    const { name, description } = body;
    return this.enterpriseService.updateIndustry(id, name, description);
  }

  // 删除行业
  @Delete('industry/delete/:id')
  async deleteIndustry(@Param('id') id: number) {
    return this.enterpriseService.deleteIndustry(id);
  }

  // 添加企业到行业
  @Post(':enterpriseId/industry/:industryId')
  async addEnterpriseToIndustry(
    @Param('enterpriseId') enterpriseId: number,
    @Param('industryId') industryId: number,
  ) {
    return this.enterpriseService.addEnterpriseToIndustry(
      enterpriseId,
      industryId,
    );
  }

  // 删除企业到行业
  @Delete(':enterpriseId/industry/:industryId')
  async removeEnterpriseFromIndustry(
    @Param('enterpriseId') enterpriseId: number,
    @Param('industryId') industryId: number,
  ) {
    return this.enterpriseService.removeEnterpriseFromIndustry(
      enterpriseId,
      industryId,
    );
  }

  // 添加专利
  @Post('patent/add')
  async addPatent(@Body() createPatentDto: CreatePatentDto) {
    const patent = await this.enterpriseService.addPatent(createPatentDto);
    return patent;
  }

  // 删除专利
  @Delete('patent/delete/:id')
  async deletePatent(@Param('id') id: number) {
    const patent = await this.enterpriseService.deletePatent(id);
    return patent;
  }

  // 更新专利
  @Put('patent/update')
  async updatePatent(@Body() updatePatentDto: UpdatePatentDto) {
    return await this.enterpriseService.updatePatent(updatePatentDto);
  }

  // 根据名字模糊查询专利
  @Get('patent/list')
  async findPatentsByName(
    @Query('name') name: string,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(10),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    return await this.enterpriseService.findPatentsByName(
      pageNo,
      pageSize,
      name,
    );
  }

  // 根据企业id查询专利
  @Get('patent/query')
  async findPatentsById(
    @Query('enterpriseId') enterpriseId: number,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(10),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    const id = Number(enterpriseId);
    return this.enterpriseService.findPatensById(id, pageNo, pageSize);
  }

  // 创建商标
  @Post('trademark/add')
  async addTrademark(@Body() createTrademarkDto: CreateTrademarkDto) {
    return await this.enterpriseService.createTrademark(createTrademarkDto);
  }

  // 删除商标
  @Delete('trademark/delete/:id')
  async deleteTrademark(@Param('id') id: number) {
    return await this.enterpriseService.deleteTrademark(id);
  }

  // 更新商标
  @Put('trademark/update')
  async updateTrademark(@Body() updateTrademarkDto: UpdateTrademarkDto) {
    return await this.enterpriseService.updateTrademark(updateTrademarkDto);
  }

  // 查找商标，通过名称模糊查询
  @Get('trademark/list')
  async findTrademarksByName(
    @Query('name') name: string,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(10),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    return await this.enterpriseService.findTrademarksByName(
      pageNo,
      pageSize,
      name,
    );
  }

  // 根据企业id查询商标信息
  @Get('trademark/query')
  async findTrademarksById(
    @Query('enterpriseId') enterpriseId: number,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(10),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    const id = Number(enterpriseId);
    return this.enterpriseService.findTrademarksById(id, pageNo, pageSize);
  }

  // 添加高管信息
  @Post('executive/add')
  async addExecutive(@Body() createExecutiveDto: CreateExecutiveDto) {
    const executive =
      await this.enterpriseService.addExecutive(createExecutiveDto);
    return executive;
  }

  // 删除高管信息
  @Delete('executive/delete/:id')
  async deleteExecutive(@Param('id') id: number) {
    const executive = await this.enterpriseService.deleteExecutive(id);
    return executive;
  }

  // 更新高管信息
  @Put('executive/update')
  async updateExecutive(@Body() updateExecutiveDto: UpdateExecutiveDto) {
    return await this.enterpriseService.updateExecutive(updateExecutiveDto);
  }

  // 根据企业id查询高管信息
  @Get('executive/query')
  async findExecutivesById(
    @Query('enterpriseId') enterpriseId: number,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(10),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    const id = Number(enterpriseId);
    return this.enterpriseService.findExecutivesById(id, pageNo, pageSize);
  }

  // 根据名字查询高管信息
  @Get('executive/list')
  async findExecutivesByName(
    @Query('name') name: string,
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
    pageNo: number,
    @Query(
      'pageSize',
      new DefaultValuePipe(10),
      generateParseIntPipe('pageSize'),
    )
    pageSize: number,
  ) {
    return await this.enterpriseService.findExecutivesByName(
      pageNo,
      pageSize,
      name,
    );
  }

  // 企业风险评估
  @Get('risk/:enterpriseId')
  async calculateRisk(@Param('enterpriseId') enterpriseId: number) {
    return await this.enterpriseService.calculateRiskScoreByEnterpriseId(
      enterpriseId,
    );
  }

  // 收藏
  @Post('favorite/:id/:userId')
  @RequireLogin()
  async addFavorite(@Param('userId') userId: number, @Param('id') id: number) {
    return await this.enterpriseService.addFavorite(userId, id);
  }

  // 取消收藏
  @Delete('favorite/:id/:userId')
  @RequireLogin()
  async removeFavorite(
    @Param('userId') userId: number,
    @Param('id') id: number,
  ) {
    return await this.enterpriseService.removeFavorite(userId, id);
  }

  // 查询收藏
  @Get('favorite/:userId')
  @RequireLogin()
  async getFavorites(@Param('userId') userId: number) {
    return await this.enterpriseService.getFavorites(userId);
  }
}
